#include<stdio.h>
#include<stdlib.h>

typedef struct YlistNode{
	int data;
	struct YlistNode *next;
} YListNode;

typedef struct Ylist
{
	YListNode *head;
	YListNode *tail;
} YList;

typedef struct XlistNode{
	int data;
	struct Ylist y;
	struct XlistNode *next;
} XListNode;

typedef struct list
{
	XListNode *head;
	XListNode *tail;
} List;

void list_empty(List *lst)
{
	lst->head = lst->tail = NULL;
}

void listy_empty(YList *lst)
{
	lst->head = lst->tail = NULL;
}

void freeList(List *lst)
{
	XListNode *p, *temp;
	YListNode *py, *tempy;
	p = lst->head;
	while(p)
	{
		py = p->y.head;
		while(py)
		{
			tempy = py->next;
			free(py);
			py = tempy;
		}
		temp = p->next;
		free(p);
		p = temp;
	}
}

void printList(List lst)
{
	printf("Printing the list:\n");
	XListNode *p;
	YListNode *py;
	p = lst.head;
	while(p)
	{
		py = p->y.head;
		while(py)
		{
			printf("(%d,%d) ",p->data,py->data);
			py = py->next;
		}
		printf("\n");
		p = p->next;
	}
}

void list_insertx(List *lst, int data)
{
	XListNode *p, *temp;
	if(!lst->head)
	{
		lst->head = malloc(sizeof(XListNode));
		lst->head->data = data;
		lst->head->next = NULL;
		listy_empty(&(lst->head->y));
		lst->tail = lst->head;
		return;
	}
	if(data < lst->head->data)
	{
		temp = malloc(sizeof(XListNode));
		temp->data = data;
		listy_empty(&(temp->y));
		temp->next = lst->head;
		lst->head = temp;
		return;
	}
	p = lst->head;
	while(p->next)
	{
		if(data == p->data) return;
		if((data > p->data) && (data < p->next->data))
		{
			temp = p->next;
			p->next = malloc(sizeof(XListNode));
			listy_empty(&(p->next->y));
			p->next->data = data;
			p->next->next = temp;
			return;
		}
		p = p->next;
	}
	p->next = malloc(sizeof(XListNode));
	listy_empty(&(p->next->y));
	p->next->data = data;
	p->next->next = NULL;
}

int list_inserty(YList *lst, int data)
{
	YListNode *p, *temp;
	if(!lst->head)
	{
		lst->head = malloc(sizeof(YListNode));
		lst->head->data = data;
		lst->head->next = NULL;
		lst->tail = lst->head;
		return 0;
	}
	if(data < lst->head->data)
	{
		temp = malloc(sizeof(YListNode));
		temp->data = data;
		temp->next = lst->head;
		lst->head = temp;
		return 0;
	}
	p = lst->head;
	while(p->next)
	{
		if(data == p->data)
		{
			temp = p->next;
			p->next = malloc(sizeof(YListNode));
			p->next->data = data;
			p->next->next = temp;
			return 1;
		}
		if((data > p->data) && (data < p->next->data))
		{
			temp = p->next;
			p->next = malloc(sizeof(YListNode));
			p->next->data = data;
			p->next->next = temp;
			return 0;
		}
		p = p->next;
	}
	p->next = malloc(sizeof(YListNode));
	p->next->data = data;
	p->next->next = NULL;
	return 0;
}

List getCoordList()
{
	List ret;
	XListNode *p;
	int count, input;
	list_empty(&ret);
	scanf("%d", &count);
	for(int i = 0; i < count; i++)
	{
		scanf("%d", &input);
		list_insertx(&ret, input);
		p = ret.head;
		while((p->data < input) && (p))
			p = p->next;
		scanf("%d", &input);
		list_inserty(&(p->y), input);

	}

	return ret;
}

unsigned int getPairOccurrences(List lst, int x, int y)
{
	XListNode *p;
	YListNode *py;
	unsigned int count = 0;
	p = lst.head;
	while(p->data != x && p)
		p = p->next;
	py = p->y.head;

	while(py)
	{
		if(py->data == y) count++;
		py= py->next;
	}

	return count;
}

unsigned int getXOccurrences(List lst, int x)
{
	unsigned int count;

	XListNode *p;
	YListNode *py;

	count = 0;

	p = lst.head;
	while(p)
	{
		if(p->data == x)
		{
			py = p->y.head;
			while(py)
			{
				count++;
				py = py->next;
			}
		}
		p = p->next;
	}

	return count;
}

int insertCoordinate(List *lst, int x, int y)
{
	XListNode *p;
	list_insertx(lst, x);
	p = lst->head;
	while(p && p->data != x)
		p = p->next;
	return list_inserty(&(p->y), y);


}

int main()
{

	List coordList;

	int x, y;
	int res;

	coordList = getCoordList();
	printList(coordList);

	// get the (x,y) to insert
	scanf("%d%d", &x, &y);

	res = insertCoordinate(&coordList, x, y);

	printList(coordList);
	if(res == 0)
		printf("The point (%d,%d) didn't appear\n",x,y);
	else
		printf("The point (%d,%d) already appeared\n",x,y);

	freeList(&coordList);

}
